
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href='/css/styles.css' rel='stylesheet' type='text/css' />
    <link href='/images/favicon.png' rel='shortcut icon' />
    <script src='/js/jquery.min.1.4.js'></script>
    <script src='/js/app.js'></script>
    <meta content='width=device-width, minimum-scale=1.0, maximum-scale=1.0' name='viewport' />
    <title>Event Library – Redis中文资料站</title>
	<meta http-equiv="description" content="redis中文资料站，下载安装redis，查找redis常用命令（commands），选择适合的redis客户端方式，配置redis主从（master-slave），阅读redis官方文档，社区里了解更多redis信息，提交redis的bug。" />
  </head>
  <body class=''>
    <script src='/js/head.js'></script>
    <div class='text'>
      <article id='topic'>
        <h1>Event Library</h1>
        
        <h2>Why is an Event Library needed at all?</h2>
        
        <p>Let us figure it out through a series of Q&amp;As.</p>
        
        <p>Q: What do you expect a network server to be doing all the time? <br/>
        A: Watch for inbound connections on the port its listening and accept them.</p>
        
        <p>Q: Calling <a href="http://man.cx/accept%282%29%20accept">accept</a> yields a descriptor. What do I do with it?<br/>
        A: Save the descriptor and do a non-blocking read/write operation on it.</p>
        
        <p>Q: Why does the read/write have to be non-blocking?<br/>
        A: If the file operation ( even a socket in Unix is a file ) is blocking how could the server for example accept other connection requests when its blocked in a file I/O operation.</p>
        
        <p>Q: I guess I have to do many such non-blocking operations on the socket to see when it's ready. Am I right?<br/>
        A: Yes. That is what an event library does for you. Now you get it.</p>
        
        <p>Q: How do Event Libraries do what they do?<br/>
        A: They use the operating system's <a href="http://www.devshed.com/c/a/BrainDump/Linux-Files-and-the-Event-Poll-Interface/">polling</a> facility along with timers.</p>
        
        <p>Q: So are there any open source event libraries that do what you just described? <br/>
        A: Yes. <code>libevent</code> and <code>libev</code> are two such event libraries that I can recall off the top of my head.</p>
        
        <p>Q: Does Redis use such open source event libraries for handling socket I/O?<br/>
        A: No. For various <a href="http://groups.google.com/group/redis-db/browse_thread/thread/b52814e9ef15b8d0/">reasons</a> Redis uses its own event library.</p>
      </article>
    </div>
    <div class='text' id='comments'>
      <div id='disqus_thread'></div>
      <script type='text/javascript'>
        //<![CDATA[
          var disqus_shortname = 'rediscn';
          
          /* * * DON'T EDIT BELOW THIS LINE * * */
          (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
              dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
              (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
          })();
        //]]>
      </script>
      <a class='dsq-brlink' href='http://disqus.com'>
        Comments powered by
        <span class='logo-disqus'>
          Disqus
        </span>
      </a>
    </div>
    <script src='/js/foot.js'></script>
  </body>
</html>
